home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 6 / ddj_cspc.zip / STEVENS.LST < prev    next >
File List  |  1989-11-21  |  3KB  |  192 lines

  1. _C PROGRAMMER'S GUIDE TO C++_
  2. by Al Stevens
  3.  
  4. Example 1: Global variables
  5.  
  6.   int amount = 123;
  7.   main()
  8.   {
  9.       int amount = 456;
  10.       printf("%d", ::amount);
  11.       printf("%d", amount);;
  12.   }
  13.  
  14.  
  15. Example 2: Structures with functions
  16.  
  17.   struct cube {
  18.       int length;
  19.       int width;
  20.       int height;
  21.       int volume();
  22.   };
  23.  
  24.  
  25. Example 3: A class definition
  26.  
  27.   class date {
  28.       int day;
  29.       int month;
  30.       int year;
  31.   public:
  32.       date(void)
  33.         {day = month = year = 0;}
  34.       date(int da, int mo, int yr);
  35.       ~date() { /* null destructor */ }
  36.       void display(void);
  37.   };
  38.  
  39.  
  40. Example 4: Constructor function
  41.  
  42.   date::date(int da, int mo, int yr)
  43.   {
  44.       day = da;
  45.       month = mo;
  46.       year = yr;
  47.   }
  48.  
  49.  
  50. Example 5: Making an assignment of friend status
  51.  
  52.   class time;
  53.   class date {
  54.     // ...
  55.     friend void now(date&, time&);
  56.   };
  57.   class time {
  58.     // ...
  59.     friend void shownow(date&, time&);
  60.   };
  61.  
  62.  
  63.  
  64. Example 6: A simple date class
  65.  
  66.  
  67.   date retirement_date, today;
  68.   // ...
  69.   if (retirement_date < today)
  70.       // Keep working ...
  71.  
  72. How about this?
  73.  
  74.   date date_married, today;
  75.   // ...
  76.   if (date_married + 365 == today)
  77.       // Happy Anniversary ...
  78.  
  79.  
  80. Example 7: A member function
  81.  
  82.   int date::operator<(date& dt)
  83.   {
  84.       if (year < dt.year)
  85.           return TRUE;
  86.       if (year == dt.year)   {
  87.           if (month < dt.month)
  88.               return TRUE;
  89.           if (month == dt.month)
  90.               if (day < dt.day)
  91.                   return TRUE;
  92.       }
  93.       return FALSE;
  94.   }
  95.  
  96.  
  97. Example 8: An overloaded function
  98.  
  99.   long date::operator long()(void)
  100.   {
  101.       long days_since_creation;
  102.       // compute the number of days ...
  103.       // ...
  104.       return days_since_creation;
  105.   }
  106.  
  107.  
  108. Example 9: One way of calling the function listed in Example 8
  109.  
  110.   void main()
  111.   {
  112.       date today;
  113.       long eversince;
  114.       // ...
  115.       eversince = (long)today;
  116.       eversince = today;
  117.       eversince = long(today);
  118.   }
  119.  
  120.  
  121. Example 10: Member functions
  122.  
  123.   class workday : date {
  124.   public:
  125.       // ...
  126.       int isXmas(void);
  127.   };
  128.  
  129.   int workday::isXmas(void)
  130.   {
  131.      return month == 12 && day == 25;
  132.   }
  133.  
  134.  
  135. Example 11: Non-member functions
  136.  
  137.   class workday : public date {
  138.       // ...
  139.   };
  140.  
  141.   void main()
  142.   {
  143.       workday proj_dt;
  144.       // ...
  145.       int yr_comp = proj_dt.year;
  146.   }
  147.  
  148.  
  149. Example 12: Inheriting attributes of multiple base classes
  150.  
  151.   class date {
  152.       // ...
  153.   };
  154.  
  155.   class time {
  156.       // ...
  157.   };
  158.  
  159.   class datetime : date, time {
  160.       // ...
  161.   public:
  162.       datetime(int da,int mo,int yr,
  163.                int hr,int min, int sec);
  164.   };
  165.  
  166.  
  167. Example 13: Examples of C++ virtual functions 
  168.  
  169.   class date {
  170.       // ...
  171.   public:
  172.       virtual int isXmas(void);
  173.   };
  174.  
  175.   class workday : date {
  176.   public:
  177.       int isXmas(void);
  178.   };
  179.  
  180.   void main()
  181.   {
  182.       date dt;
  183.       workday wd;
  184.       date *dat = &dt;
  185.       // ...
  186.       wd.isXmas();       // workday::isXmas
  187.       dt.isXmas();       // date::isXmas
  188.       dat->isXmas();     // workday::isXmas (!)
  189.       wd.date::isXmas(); // date::isXmas
  190.   }
  191.  
  192.